/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.milib.animation.static_ {
	import com.googlecode.milib.vo.MLEvent;

	public class AnimationElement {
		
		public var obj : Object;
		public var param : String;
		public var target : Number;
		public var ease : MLEase;
		public var isMirrorEase : Boolean;
		private var init : Number;
		private var dist : Number;
		public var isBlocked : Boolean;
		private var runningIn:Animation;

		public function AnimationElement(obj:Object, param:String, target:Number, ease:MLEase=null) {
			this.obj=obj;
			this.param=param;
			this.target=target;
			this.ease= ease==null ? (new MLEase()).setupEaseSin() : ease;
			ObjectAnimations.forInstance(obj).regAnimationElement(this);
		}//<>
		
		public function reset():void {
			init=obj[param];			dist=target-init;
			isBlocked=false;
			ObjectAnimations.forInstance(obj).regRunningAnimationElement(this);
		}
		
		public function applyN01(n01:Number):void {
			if(isBlocked){ return; }
			if(isMirrorEase){
				obj[param]=init+dist*ease.getEaseWithMirrorEase(n01);
			}else{
				obj[param]=init+dist*ease.getEase(n01);
			}
		}
		
		public function applyTargetParam():void {
			if(isBlocked){ return; }
			obj[param]=target;
		}
		
		public function clone():AnimationElement {
			var ae:AnimationElement=new AnimationElement(obj, param, target, ease);
			return ae;
		}
		
		public function cloneByTargetAsCurrent(isMirrorEase:Boolean=false):AnimationElement {
			var ae:AnimationElement=new AnimationElement(obj, param, obj[param], ease);
			ae.isMirrorEase=isMirrorEase;
			return ae;
		}
		
		public function cloneByNewObject(newObject:Object):AnimationElement {
			var ae:AnimationElement=new AnimationElement(newObject, param, target, ease);
			return ae;
		}
		
		public function setRunningInAnimation(runningIn:Animation):void {
			if(this.runningIn){
				if(this.runningIn==runningIn){ return; }
				this.runningIn.removeEventListener(this.runningIn.event_ActionStart, onRunningInActionStart);
				this.runningIn.finish();
			}
			this.runningIn=runningIn;
			this.runningIn.addEventListener(this.runningIn.event_ActionStart, onRunningInActionStart);
		}
		
		public function terminateIfIsRunningInAnimation():void {
			if(runningIn){
				runningIn.finish();
			}
		}
		
//********************************************************************************************
//		events for AnimationElement
//********************************************************************************************
		protected function onRunningInActionFinish(event:MLEvent):void {
			runningIn.removeEventListener(runningIn.event_ActionFinish, onRunningInActionFinish);
			if(event.info.hero==runningIn){
				runningIn=null;
			}
		}
		
		protected function onRunningInActionStart(event:MLEvent):void {
			Animation(event.info.hero).addEventListener(runningIn.event_ActionFinish, onRunningInActionFinish);
		}
		
	}
}